using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using SubsidyApp.Models;

namespace SubsidyApp.Controllers
{

    public class DistributcionFormViewModel
    {
        // Properties
        public site_distribution site_distribution { get; private set; }
        public SelectList Zones { get; private set; }

        // Constructor
        public DistributcionFormViewModel(site_distribution distrib)
        {
            site_distribution = distrib;
            Zones = new SelectList(BasesDescription.db.site_zones.AsEnumerable(),"id","zone_title",site_distribution.zone_id);
        }
    }

    public class ContentFormViewModel
    {
        public site_content site_content { get; private set; }
        public SelectList Distributcions { get; private set; }

        public ContentFormViewModel(site_content content)
        {
            site_content = content;
            Distributcions = new SelectList(BasesDescription.db.site_distributions.AsEnumerable(), "id", "distrid_title", site_content.distrib_id);
        }
    }

    public class ZoneController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(int id, int? distrib_id)
        {
            ViewData.Model = BasesDescription.contentRepository.GetZone(id);
            IQueryable<site_distribution> DistribList = BasesDescription.contentRepository.GetZoneDistributions(id);

            if ((DistribList.Count() == 1) && (!distrib_id.HasValue))
                return RedirectToAction("List", "Zone", new { id = id, distrib_id = DistribList.Single(d => d.zone_id == id).id });

            if (distrib_id.HasValue)
                ViewData["Pages"] = BasesDescription.contentRepository.GetPages(id, distrib_id.GetValueOrDefault());

            if (DistribList.Count() > 1)
                ViewData["Distributions"] = DistribList;
            else
                ViewData["Distributions"] = null;

            if ((DistribList.Count() > 1) && (!distrib_id.HasValue))
                return RedirectToAction("List", "Zone", new { id = id, distrib_id = DistribList.ToArray()[0].id });

            return View();
        }

        public ActionResult Detail(int id)
        {
            ViewData.Model = BasesDescription.contentRepository.GetPage(id);
            return View();
        }
    }
}
